package club.itstudio.IContainer.controller.student;

import club.itstudio.IContainer.annotation.LoginRequired;
import club.itstudio.IContainer.annotation.StudentRequired;
import club.itstudio.IContainer.entity.CourseExperiment;
import club.itstudio.IContainer.entity.StudentCourse;
import club.itstudio.IContainer.entity.User;
import club.itstudio.IContainer.exception.ParamInvalidException;
import club.itstudio.IContainer.service.AuthService;
import club.itstudio.IContainer.service.CourseExperimentService;
import club.itstudio.IContainer.service.StudentCourseService;
import club.itstudio.IContainer.util.Result;
import club.itstudio.IContainer.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/student/experiment")
public class StudentCourseExperimentController {

    private StudentCourseService studentCourseService;
    private CourseExperimentService courseExperimentService;
    private AuthService authService;

    @Autowired
    public void setAuthService(AuthService authService) {
        this.authService = authService;
    }

    @Autowired
    public void setStudentCourseService(StudentCourseService studentCourseService) {
        this.studentCourseService = studentCourseService;
    }

    @Autowired
    public void setCourseExperimentService(CourseExperimentService courseExperimentService) {
        this.courseExperimentService = courseExperimentService;
    }

    @GetMapping
    @LoginRequired
    @StudentRequired
    public Result getExperiment(@RequestParam int courseID) {
        User user = authService.threadLocalUsers.get();
        StudentCourse studentCourse = studentCourseService.getByUserIDAndCourseID(user.id, courseID);
        if (studentCourse == null) throw new ParamInvalidException("不存在该课程");

        List<CourseExperiment> experiments = courseExperimentService.getByCourseID(courseID);
        return ResultUtil.success(experiments);
    }
}
